package com.endlessblue.visa.service.impl;

import org.springframework.stereotype.Service;

import com.endlessblue.visa.service.SequenceService;
import com.endlessblue.visa.util.DateUtil;
import com.endlessblue.visa.util.PropertyUtil;
import com.endlessblue.visa.util.StringUtil;

@Service
public class SequenceServiceImpl implements SequenceService {
    
	private static int SEQ = 0;
	private static int MAX_SEQ = 1000;
	
	@Override
	public String getCommonSequence() {
		StringBuilder pre = new StringBuilder();
		pre.append(DateUtil.getDate("yyyyMMddHHmmssSSS"));
		String sysNo = PropertyUtil.getValue("business", "SYS_NO");
		if (sysNo == null) {
			sysNo = "01";
		}
		pre.append(sysNo);
		pre.append(StringUtil.leftZero(getSeq(), 4));
		return pre.toString();
	}
	
	
	private synchronized String getSeq() {
		String rtn =String.valueOf(SEQ++);
		if (SEQ > MAX_SEQ) {
			SEQ = 0;
		}
		return rtn;
	}
}
